package cn.wangxl.vertx.starter.web;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

/**
 * 组测样例：
 *
 */
public class BlockingVerticle extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    Route route = router.route("/get/order").method(HttpMethod.GET);

    route.blockingHandler(ctx->{
      for(int i=0;i<6;i++){
        try {
          System.out.println("在查询用户信息中-------");
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("查询用户信息结束-------");

      ctx.data().put("order","查询用户信息结束;");
      ctx.next();
    },false).blockingHandler(ctx->{
      for(int i=0;i<6;i++){
        try {
          System.out.println("在查询订单中-------");
          Thread.sleep(500);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("查询订单结束-------");
      // 聚合消息返回
      ctx.response().end(ctx.data().get("order").toString()+"查询订单结束");

    },false);


    server.requestHandler(router).listen(8888, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }


  public static void main(String[] args) throws Exception {
    new BlockingVerticle().start(Future.future());
  }
}
